import { getVisitorList, deleteVisitor } from '../../model/visitor';
import { genderMap, relationMap } from '../../menus/index';

const app = getApp();

Page({
  data: {
    type: '',
    list: [] as any
  },
  addVisitor() {
    wx.navigateTo({
      url: '/pages/visitor/visitor?statue=add'
    });
  },
  edit(event: any) {
    const id = event.target.dataset.id;
    wx.navigateTo({
      url: '/pages/visitor/visitor?statue=edit&id=' + id
    });
  },
  selectVisitor(event: any) {
    if (this.data.type !== 'select') return;
    const info = event.currentTarget.dataset;
    app.globalData.pageData = info;
    wx.navigateBack();
  },
  async delete(event: any) {
    wx.showModal({
      title: '提示',
      content: '确定要删除该就诊人？',
      success: async (res) => {
        if (res.confirm) {
          const id = event.target.dataset.id;
          const res = await deleteVisitor({id});
          if (res.code === 200) {
            this.getVisitorList();
          }
        }
      }
    });
    
  },
  async getVisitorList() {
    const openId = app.globalData.openId;
    const data = await getVisitorList({ open_id: openId });
    data.map(item => {
      item.gender = genderMap[item.gender as 1];
      item.relation = relationMap[item.relation as 1];
    });
    this.setData({
      list: data
    });
  },
  onLoad(query) {
    const type = query.type;
    this.getVisitorList();
    this.setData({
      type
    });
  }
});

export {};